<template>
    <div class="User-container content-wrapper">

        <div class="filter-wrapper">
            <el-form ref="filterForm" :model="listQuery" :inline="true">
                <el-form-item prop="outTradeNo" size="medium">
                    <el-input v-model="listQuery.outTradeNo" placeholder="商户订单号"></el-input>
                </el-form-item>

                <el-form-item prop="payType" size="medium">
                    <el-select v-model="listQuery.payType" placeholder="支付类型" clearable style="width: 120px">
                        <el-option
                                v-for="item in payTypeOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item prop="payStatus" size="medium">
                    <el-select v-model="listQuery.payStatus" placeholder="支付状态" clearable style="width: 120px">
                        <el-option
                                v-for="item in payStatusOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item prop="orderBy" size="medium">
                    <el-select v-model="listQuery.orderBy" placeholder="排序" style="width: 120px">
                        <el-option
                                v-for="item in sortOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button type="primary" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
                <el-button type="primary" size="medium" icon="el-icon-mars-reset" @click="handleResetFilter">重置</el-button>
                <el-button type="danger" size="medium" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
            </el-form>

        </div>

        <el-table
                :data="list"
                border fit
                highlight-current-row
                v-loading="listLoading"
                element-loading-text="给我一点时间"
                @selection-change="handleSelectionChange">

            <el-table-column
                    type="selection"
                    width="55" align="center">
            </el-table-column>
            <el-table-column type="index" width="55" align="center"></el-table-column>

            <el-table-column label="支付类型" width="120px" align="center">
                <template slot-scope="scope">
                    <el-tag type="primary" v-if="scope.row.payType === 1">线上支付</el-tag>
                    <el-tag type="danger" v-if="scope.row.payType === 2">线下支付</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="商户订单号" align="center">
                <template slot-scope="scope"> {{scope.row.outTradeNo}} </template>
            </el-table-column>

            <el-table-column label="商品描述" align="center">
                <template slot-scope="scope"> {{scope.row.body}} </template>
            </el-table-column>

            <el-table-column label="金额" align="center">
                <template slot-scope="scope"> <el-tag type="primary">￥ {{scope.row.totalFee}}</el-tag> </template>
            </el-table-column>

            <el-table-column label="付款银行" align="center">
                <template slot-scope="scope"> {{scope.row.bankType}} </template>
            </el-table-column>

            <el-table-column label="支付状态" width="120px" align="center">
                <template slot-scope="scope">
                    <el-tag type="danger" v-if="scope.row.payStatus === 1">未支付</el-tag>
                    <el-tag type="primary" v-if="scope.row.payStatus === 2">已支付</el-tag>
                    <el-tag type="success" v-if="scope.row.payStatus === 3">支付完成</el-tag>
                    <el-tag type="warning" v-if="scope.row.payStatus === 4">取消支付</el-tag>
                    <el-tag type="danger" v-if="scope.row.payStatus === 5">支付失败</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="支付完成时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.timeEnd}} </template>
            </el-table-column>

            <el-table-column label="操作" width="250px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="primary" icon="edit" @click="handleDetail(scope.row)">详情</el-button>
                    <el-button size="small" type="danger" icon="delete" @click="handleRefund(scope.row)">退款</el-button>
                    <el-button size="small" type="danger" icon="delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="dialogStatus" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" @close="handleDialogClose">
            <el-form ref="form" :model="form" label-width="120px">
                <el-form-item label="标题" prop="outTradeNo">
                    <el-input v-model="form.outTradeNo" style="width: 400px;"></el-input>
                </el-form-item>

                <el-form-item label="内容" style="width: 520px;">
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 4, maxRows: 4}"
                            placeholder="请输入内容"
                            v-model="form.content">
                    </el-input>
                </el-form-item>

                <el-form-item label="状态" style="width: 300px;">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">正常</el-radio>
                        <el-radio :label="2">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item>
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
    import { created, updated, deleted, deletedBatch, selectOne, selectList, refund } from '@/api/xczs/goods_order'

    const sortOptions = [{label: '升序', key: 'ctime asc'}, {label: '降序', key: 'ctime desc'}]
    const payStatusOptions = [
        {label: '未支付', key: 1},
        {label: '已支付', key: 2},
        {label: '支付完成', key: 3},
        {label: '取消支付', key: 4},
        {label: '支付失败', key: 5}
    ]
    const payTypeOptions = [
        {label: '线上支付', key: 1},
        {label: '线下支付', key: 2}
    ]

    export default {
        name: 'GoodsOrder',
        data() {
            return {
                list: [],
                total: null,
                listLoading: false,
                multipleSelection: [],
                listQuery: {
                    page: 1,
                    rows: 10,
                    outTradeNo: undefined,
                    payStatus: undefined,
                    payType: undefined,
                    orderBy: 'ctime desc'
                },
                dialogFormVisible: false,
                dialogStatus: '',
                payStatusOptions,
                payTypeOptions,
                sortOptions,
                form: {

                }
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            handleFilter() {//条件过滤
                this.listQuery.page = 1
                this.getTableData()
            },
            handleResetFilter() {//重置过滤条件
                this.$refs.filterForm.resetFields()
                this.getTableData()
            },
            handleDialogClose() {//关闭窗口回调函数
                this.resetForm()
            },
            handleSelectionChange(val) {//多选
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getTableData()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getTableData()
            },
            handleDetail(row) {//添加数据窗口
                this.dialogStatus = row.body + ' [' + row.outTradeNo + ']'
                this.dialogFormVisible = true
                /*selectOne(row.id).then(response => {
                    this.dialogStatus = 'update'
                    this.dialogFormVisible = true
                    this.form = Object.assign({}, response.data)
                })*/
            },
            handleRefund(row) {
                refund(row.outTradeNo).then(response => {
                    console.log(response)
                })
            },
            handleDelete(row) {//删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleted(row.id).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getTableData()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'warning', message: '已取消删除' })
                })
            },
            handleBatchDelete() {//批量删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(this.multipleSelection.length > 0) {
                        let ids = []
                        for(let item of this.multipleSelection) {
                            ids.push(item.id)
                        }
                        deletedBatch({ids: ids.join(',')}).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTableData()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })

                    } else {
                        this.$notify({
                            title: '提示', message: '请选择需要删除的记录',
                            type: 'warning', duration: 2000
                        })
                    }
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            getTableData() {
                this.listLoading = true
                selectList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch(error => {
                    console.log(error)
                    this.listLoading = false
                })
            },
            resetForm() {//重置表单
                this.$refs['form'].resetFields()
                this.form = {
                    title: '',
                    content: '',
                    identity: '',
                    status: 1
                }
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>
